# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _


class SaleOrderLine(models.Model):
    _inherit = 'fp.sale.order.line'

    product_state = fields.Selection(
        related='product_id.nc_state', string='product NC Status', store=True)


class FapiaoProduct(models.Model):
    _inherit = "fp.product"

    nc_state = fields.Selection([
        ('draft', 'Draft'),
        ('done', 'Done'),
        ('error', 'Error'),
    ], string='Nc Status',  compute='_compute_nc_state', store=True)

    nc_product_ids = fields.One2many(
        'fp.nc.ods.product', 'product_id', string='nc.ods.product', readonly=True)

    @api.depends('nc_product_ids.state')
    def _compute_nc_state(self):
        for rec in self:
            if rec.nc_product_ids:
                rec.nc_state = rec.nc_product_ids[0].state
            else:
                rec.nc_state = 'draft'

    def action_done(self):
        self2 = self.search(
            [('id', 'in', self.ids), ('nc_state', '=', 'draft')])

        for one in self2:
            nc_product_ids = one.mapped('nc_product_ids')
            if nc_product_ids:
                nc_product_ids.action_done()
            else:
                one.nc_state = 'done'

    def action_draft(self):
        self2 = self.search([('id', 'in', self.ids),
                             ('nc_state', 'in', ['done', 'error'])])

        for one in self2:
            nc_product_ids = one.mapped('nc_product_ids')
            if nc_product_ids:
                nc_product_ids.action_draft()
            else:
                one.nc_state = 'draft'

    def search_by_code(self, code):
        if code == '50000000544':
            return self.env.ref('hw_fapiao.product_discount')

        return self.search([('code', '=', code)], limit=1)

    def discount_get(self):
        return self.env.ref('hw_fapiao.product_discount')


class NcOdsProduct(models.Model):
    _name = "fp.nc.ods.product"
    _description = "NC ODS Product"

    state = fields.Selection([
        ('draft', 'Draft'),
        ('done', 'Done'),
        ('error', 'Error'),
    ], string='Status', readonly=True, copy=False, index=True, default='draft')

    product_id = fields.Many2one(
        'fp.product', string='Product', readonly=True, index=True)

    company_code = fields.Char('公司代码')
    company_name = fields.Char('公司名称')

    name = fields.Char(string='商品名称')
    code = fields.Char(string='商品编码')
    spec = fields.Char(string='规格型号',)
    uom = fields.Char(string='单位',)

    vat_product_code = fields.Char(string='商品类别编码',)
    vat_product_name = fields.Char(string='商品类别')

    tax_ratio = fields.Float(string='tax_ratio',)
    price_unit = fields.Float('Unit Price', default=0.0)

    def action_done(self):
        self2 = self.search([('id', 'in', self.ids), ('state', '=', 'draft')])
        for one in self2:
            one.write({'state': 'done'})

    def action_draft(self):
        self2 = self.search(
            [('id', 'in', self.ids), ('state', 'in', ['done', 'error'])])
        for one in self2:
            one.write({'state': 'draft'})

    def _load_to_ods(self):
        vat_prd = self.env['fp.vat.product']
        prd = self.env['fp.product']

        for one in self:

            # prd2 = prd.search([('code', '=', one.code)], limit=1)
            prd2 = prd.search_by_code(one.code)

            if not prd2:
                vals = {
                    'in_out': 'out',
                    'name': one.name,
                    'code': one.code,
                    'spec': one.spec,
                    'uom': one.uom,
                    'price_unit': one.price_unit,
                }

                vat_prd2 = vat_prd.search(
                    [('code', '=', one.vat_product_code)], limit=1)

                if vat_prd2:
                    vals.update({
                        'vat_product_id': vat_prd2.id,
                    })
                else:
                    vals.update({
                        'vat_product_code': one.vat_product_code,
                        'vat_product_name': one.vat_product_name,
                    })

                prd2 = prd.create(vals)

                if not vat_prd2:
                    prd2.write({
                        'vat_product_code': one.vat_product_code,
                        'vat_product_name': one.vat_product_name,
                    })

            one.product_id = prd2

            prd = prd | prd2

        return prd

    @api.returns('self', lambda value: value.ids)
    def load_to_model(self):
        return self._load_to_ods()

    @api.model
    @api.returns('self', lambda value: value.ids)
    def import_create_for_nc(self, vals_list):
        records = []
        for one in vals_list:
            code = one['code']
            old = self.search([('code', '=', code)])
            if not old:
                records.append(one)

        new_result = self.create(records)

        return new_result
